<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'admininformation.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
    <link rel="stylesheet" type="text/css" href="lib/layui/css/layui.css"/>
	<script src="lib/layui/layui.js" type="text/javascript" charset="utf-8"></script>
  </head>
  <script type="text/html" id="barProduct">
   <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="toolbarPro">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm" lay-event="addpro">添加新闻</button>
    <button class="layui-btn layui-btn-sm" lay-event="dels">批量删除</button>
  </div>
</script>
 <script type="text/javascript">
 layui.use(['table','jquery','form','upload'], function(){
  var table = layui.table;
   var form=layui.form;
  var $ = layui.jquery;
  var upload=layui.upload;
  $.post('infomation',{
   
   order:2
   },function(res){
	
		var selptid=document.getElementById("ptid");
		for(var i=0;i<res.length;i++){
		var op=document.createElement("option");
		op.text=res[i].ntype;
		op.value=res[i].nid;
		selptid.add(op);
		}
	form.render(); 
   },'json');
     $.post('infomation',{
   
   order:2
   },function(res){
	
		var selptid=document.getElementById("updateptid");
		for(var i=0;i<res.length;i++){
		var op=document.createElement("option");
		op.text=res[i].ntype;
		op.value=res[i].nid;
		selptid.add(op);
		}
	form.render(); 
   },'json');
    //表单监听提交	
		form.on('submit(addproduct)', function(data){
			//表单序列化，快速获取表单中所有name属性
			var str=$("#addproform").serialize()+"&order=3";
		  $.post('infomation',str,function(res){
		  	if(res.result.success){
		  		layer.closeAll();//关闭所有弹窗
		  		layer.msg(res.result.message);
		  		table.reload("infoReload");
		  		$("#addproform")[0].reset();
		  		from.render();
		  	}else{
		  		layer.msg(res.result.message);
		  	}
		  },"json")
		  return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
		 });
   table.on('toolbar(infotable)', function(obj){
		    var checkStatus = table.checkStatus(obj.config.id);
		    switch(obj.event){
		      case 'addpro':
		      layer.open({
		        	type:1,
		        	content:$("#addproform")
		        	,area:['800px','500px']
		        	 ,offset: ['100px']
		        }) 
		         
		      break;
		      case 'dels':
		       
   
    var data=checkStatus.data;
      var arrpids=new Array(data.length);
      for(var i=0;i<data.length;i++){
      arrpids[i]=data[i].pid;
      }
      	layer.confirm('真的删除这些行么', function(index) {
      $.post('adminproductServlet',{
 				arrpids:arrpids,
				order:6
 			},function(res){
 				if(res.result.success){
 				layer.close(index);
 					layer.msg(res.result.message);
 					table.reload("infoReload");
 				}else{
 					layer.msg(res.result.message);
 				}
 			},"json");
 			});
		      break;
		     
		   
		    };
		  });
		  
		 
  //方法级渲染
  table.render({
    elem: '#infotable'
    ,url: 'infomation'
    ,toolbar: '#toolbarPro' //开启头部工具栏，并为其绑定左侧模板
    ,cols: [[
      {checkbox: true, fixed: true}
      //,{field:'newid', title: 'ID',sort: true, fixed: true}
      ,{field:'title', title: '新闻类别', width:150}
      ,{field:'content', title: '新闻内容', width:200}
      ,{field:'createtime', title: '日期', width:120,sort: true}
      //,{field:'iscarefully', title: '是否为空'}
      ,{field:'ntype', title: '新闻名称', width:150}
     ,{fixed: 'right', title:'操作', toolbar: '#barProduct',width: 200}
    ]]
    ,id: 'infoReload'
    ,page: true
     ,where:{
    	order:1
    }
  });
  table.on('tool(infotable)', function(obj) {
  	var data = obj.data;
  	//console.log(obj)
  	if(obj.event === 'del') {
  		layer.confirm('真的删除行么', function(index) {
 			$.post('infomation',{
 				newid:data.newid
				,order:5
 			},function(res){
 				if(res.result.success){
 				layer.close(index);
 					layer.msg(res.result.message);
 					table.reload("infoReload");
 				}else{
 					layer.msg(res.result.message);
 				}
 			},"json");
  		});
  	}  else if(obj.event === 'edit') {
  		 layer.open({
			type:1, 
			content:$("#updateproform")
			,area: ['700px', '400px']
			, 
			title:"您正在修改类型的是 ["+data.title+"]的信息"
			});
			 //给表单赋值
				form.val("updateproform", {
				  "title":data.title
				 ,"content":data.content
				 ,"ptid":data.ntid
				 ,"pid":data.newid
				});
			//表单监听提交
			form.on('submit(updateproduct)', function(data){
			//表单序列化，快速获取表单中所有name属性
			var str=$("#updateproform").serialize()+"&order=4";
		  $.post('infomation',str,function(res){
		  	if(res.result.success){
		  		layer.closeAll();//关闭所有弹窗
		  		layer.msg(res.result.message);
		  		table.reload("infoReload");
		  		$("#updateproform")[0].reset();//清空表单
		  		form.render();
		  	}else{
		  		layer.msg(res.result.message);
		  	}
		  },"json");
		  return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
		 });
    }/* else if(obj.event === 'imgs'){
    	layer.open({
			type:1, 
			content:$("#imgsform")
			,area: ['700px', '400px']
			,offset:'50px'
			});
    } */
  	})
   active = {
    reload: function(){
      var title = $('#title');
      var iscarefully = $('#iscarefully');
      //执行重载
      table.reload('infoReload', {
        page: {
          curr: 1 //重新从第 1 页开始
        }
        ,where: {
            title: title.val(),
            iscarefully: iscarefully.val(),
            order:1
        }
      }, 'data');
    }
  };
  $('#search').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
});
 </script>
  <body>
  <form class="layui-form" id="updateproform" lay-filter="updateproform"  style="display:none;">
  <input name="pid" style="display: none;"/>
		  <div class="layui-form-item">
			     <label class="layui-form-label">咨询类别</label>
			    <div class="layui-input-inline">
			           <input name="title" class="layui-input" type="text" placeholder="请输入咨询名称" autocomplete="off" lay-verify="title">
			    </div>
			    <div class="layui-inline">
		      <label class="layui-form-label">资讯内容</label>
		      <div class="layui-input-inline">
		        <input name="content" type="text" class="layui-input"placeholder="请输入咨询内容"type="text" autocomplete="off" lay-verify="">
		      </div>
		    </div>
		    
			  </div>
			   <div class="layui-form-item">
			     <label class="layui-form-label">资讯类型</label>
			    	
					   <div class="layui-input-inline" style="width:100px;"> 
					   	    <select id="updateptid" name="ptid"> 
						</select>
		    </div>
  </div>
<div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" type="submit" lay-filter="updateproduct" lay-submit="">立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div> 
  </div>
  </form>
 <form class="layui-form" id="addproform" style="display:none;">
		  <div class="layui-form-item">
			     <label class="layui-form-label">咨询类别</label>
			    <div class="layui-input-inline">
			           <input name="title" class="layui-input" type="text" placeholder="请输入咨询名称" autocomplete="off" lay-verify="title">
			    </div>
			    <div class="layui-inline">
		      <label class="layui-form-label">资讯内容</label>
		      <div class="layui-input-inline">
		        <input name="content" type="text" class="layui-input"placeholder="请输入咨询内容"type="text" autocomplete="off" lay-verify="">
		      </div>
		    </div>
		    
			  </div>
			   <div class="layui-form-item">
			     <label class="layui-form-label">资讯类型</label>
			    	
					   <div class="layui-input-inline" style="width:100px;"> 
					   	    <select id="ptid" name="ptid"> 
						
						</select>
						
		    </div>
  </div>
   <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" type="submit" lay-filter="addproduct" lay-submit="">立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div> 
  </div>
</form>
  <div class="layui-form-item" style="margin-top: 10px; margin-left:10px">
		<form class="layui-form" method="post" onsubmit="return false;">
			<label class="layui-form-mid">类别:</label>
			<div class="layui-input-inline" style="width:100px;">
				<input type="text" id="title" name="title" autocomplete="off" class="layui-input">
			</div>
			<label class="layui-form-mid">是否轮播:</label>
			<div class="layui-input-inline" style="width:100px;">
				<select id="iscarefully" name="iscarefully">
					<option value="">请选择</option>
					<option value="true">是</option>
					<option value="false">否</option>
				</select>
			</div>
			<div class="layui-input-inline" style="width:100px;">
				<button class="layui-btn" id="search" data-type="reload">搜索</button>
			</div>
			</from>
			</div>
   <table class="layui-hide" id="infotable" lay-filter="infotable"></table> 
  </body>
</html>
